const app = getApp()
Page({
  data: {
    banner: [],
    normal: {
      list: [],
      page: 1
    },
    recommend: {
      list: [],
      page: 1,
      is_load: false
    },
    hot: {
      list: []
    },
    new: {
      list: [],
      page: 1,
      is_load: false
    },
    discount: {
      list: []
    },
    dead: false,
    show: false,
    telBlockState: false,
    getCodeText: "获取验证码",
    tel: "",
    telCode: "",
    applyMsgState: true,
    getCodeState: false,
  },
  // rowSrcroll: function(e) {
  //   let that = this;
  //   let page = 1;
  //   let listType = e.target.dataset.type;
  //   if ((listType == 4 && that.data.new.is_load) || listType == 2 && that.data.recommend.is_load) {
  //     return;
  //   }
  //   if (listType == 4) {
  //     page = that.data.new.page
  //     that.setData({
  //       'new.is_load': true
  //     })
  //   } else if (listType == 2) {
  //     page = that.data.recommend.page
  //     that.setData({
  //       'recommend.is_load': true
  //     })
  //   };
  //   ++page;
  //   wx.request({
  //     url: app.buildUrl('/shop/index/get-goods'),
  //     method: 'GET',
  //     header: app.buildHeader(),
  //     data: {
  //       goods_type: listType,
  //       page: page
  //     },
  //     success: function(res) {
  //       if (res.data.code == 200) {
  //         if (listType == 2) {
  //           if (res.data.data.length == 0) {
  //             app.showToast('没有更多了')
  //             that.setData({
  //               'recommend.is_load': true
  //             })
  //           } else {
  //             that.setData({
  //               'recommend.list': that.data.recommend.list.concat(res.data.data),
  //               'recommend.page': page,
  //               'recommend.is_load': false
  //             })
  //           }
  //         } else if (listType == 4) {
  //           if (res.data.data.length == 0) {
  //             app.showToast('没有更多了')
  //             that.setData({
  //               'new.is_load': true
  //             })
  //           } else {
  //             let newList = res.data.data;
  //             newList.forEach(function (i) {
  //               i.max_discount_price = parseFloat(i.max_discount_price).toFixed(2);
  //               i.max_original_price = parseFloat(i.max_original_price).toFixed(2);
  //               i.min_discount_price = parseFloat(i.min_discount_price).toFixed(2);
  //               i.min_original_price = parseFloat(i.min_original_price).toFixed(2);
  //             })
  //             that.setData({
  //               'new.list': that.data.new.list.concat(newList),
  //               'new.page': page,
  //               'new.is_load': false
  //             })
  //           }
  //         }
  //       }
  //     }
  //   })
  // },
  loadMore: function() {
    let that = this;
    if (that.data.dead) {
      return;
    }
    let page = that.data.normal.page;
    ++page;
    wx.request({
      url: app.buildUrl('/shop/index/get-goods'),
      method: 'GET',
      header: app.buildHeader(),
      data: {
        goods_type: 0,
        page: page
      },
      success: function(res) {
        if (res.data.code == 200) {
          if (res.data.data.length == 0) {
            app.showToast('没有更多了')
            that.setData({
              dead: true
            })
          } else {
            let newList = res.data.data;
            newList.forEach(function(i) {
              i.max_discount_price = parseFloat(i.max_discount_price).toFixed(2);
              i.max_original_price = parseFloat(i.max_original_price).toFixed(2);
              i.min_discount_price = parseFloat(i.min_discount_price).toFixed(2);
              i.min_original_price = parseFloat(i.min_original_price).toFixed(2);
              if (parseInt(i.display_sales) > 10000) {
                i.display_sales = i.display_sales / 10000 + 'w'
              }
            })
            that.setData({
              'normal.list': that.data.normal.list.concat(newList),
              'normal.page': page
            })
          }
        }
      }
    })
  },
  load: function() {
    let that = this;
    wx.request({
      url: app.buildUrl('/shop/index/get-banner'),
      method: 'GET',
      header: app.buildHeader(),
      success: function(res) {
        if (res.data.code == 200) {
          that.setData({
            banner: res.data.data
          })
        }
      }
    })
    var arr = [0, 2, 3, 4, 5];
    arr.forEach(function(i) {
      wx.request({
        url: app.buildUrl('/shop/index/get-goods'),
        method: 'GET',
        header: app.buildHeader(),
        data: {
          goods_type: i
        },
        success: function(res) {
          if (res.data.code == 200) {
            let newList = res.data.data;
            newList.forEach(function(i) {
              i.max_discount_price = parseFloat(i.max_discount_price).toFixed(2);
              i.max_original_price = parseFloat(i.max_original_price).toFixed(2);
              i.min_discount_price = parseFloat(i.min_discount_price).toFixed(2);
              i.min_original_price = parseFloat(i.min_original_price).toFixed(2);
              if (parseInt(i.display_sales) > 10000) {
                i.display_sales = i.display_sales / 10000 + 'w'
              }
            })
            if (i == 0) {
              that.setData({
                'normal.list': newList
              })
            } else if (i == 2) {
              that.setData({
                'recommend.list': newList
              })
            } else if (i == 3) {
              that.setData({
                'hot.list': newList.slice(0, 3)
              })
            } else if (i == 4) {
              that.setData({
                'new.list': newList
              })
            } else if (i == 5) {
              that.setData({
                'discount.list': newList.slice(0, 3)
              })
            }
          }
        }
      })
    })

  },
  //授权
  onGotUserInfo(e) {
    let that = this;
    if (e.detail.userInfo) {
      that.setData({
        show: false,
        telBlockState: true
      });
      wx.request({
        url: app.buildUrl('/shop/shop/save-user-info'),
        header: app.buildHeader(),
        method: 'POST',
        data: {
          url: e.detail.userInfo.avatarUrl,
          nickname: e.detail.userInfo.nickName
        },
        success: res => {
          if (res.data.code == 200) {
            // 再次请求
            wx.setStorageSync('status', 200)
            
          } else {
            app.showToast(res.data.msg)
          }
        }
      })
    }
  },
  telIpt(e) { //电话号码
    this.setData({
      tel: e.detail.value
    })
  },
  codeIpt(e) { //验证码
    this.setData({
      telCode: e.detail.value
    })
  },
  verification() { //验证手机
    let that = this;
    var tel = that.data.tel,
      telReg = /^1\d{10}$/;
    if (tel === "" || !tel) {
      app.showToast("电话号码不能为空！")
      return false;
    } else if (!telReg.test(tel)) {
      app.showToast("电话号码格式错误！")
      return false;
    } else {
      return true;
    }
  },
  telConfirm() { // 确认
    let that = this;
    if (that.verification()) {
      if (that.data.telCode == "" || !that.data.telCode) {
        app.showToast("请输入验证码！")
      } else {
        wx.request({
          url: app.buildUrl('/shop/shop/save-shop-info'),
          header: app.buildHeader(),
          method: 'POST',
          data: {
            phone: that.data.tel,
            code: that.data.telCode
          },
          success: function(res) {
            if (res.data.code == 200) {
              app.showToast(res.data.msg)
              that.setData({
                tel: "",
                telBlockState: false
              })
              // 再次请求
              wx.setStorageSync('status', 200)
              that.load();
            } else {
              app.showToast(res.data.msg)
            }
          },
          fail: function(res) {
            console.log(res)
          }
        })
      }
    }
  },
  getCodeFun() { // 获取验证码
    var that = this;
    if (that.verification()) {
      let [times, state, timer] = [90, that.data.getCodeState, null];
      if (state) {
        return
      }
      wx.request({
        url: app.buildUrl('/shop/shop/send-code'),
        header: app.buildHeader(),
        method: 'POST',
        data: {
          phone: that.data.tel
        },
        success: function(res) {
          if (res.data.code == 200) {
            that.setData({
              getCodeState: true,
              getCodeText: times + "s"
            })
            timer = setInterval(function() {
              times--;
              that.setData({
                getCodeText: times + "s"
              })
              if (times == 0) {
                clearTimeout(timer)
                timer = null;
                times = 180;
                that.setData({
                  getCodeText: "获取验证码",
                  getCodeState: false
                })
              }
            }, 1000)
            app.showToast(res.data.msg)
          } else {
            app.showToast(res.data.msg)
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    }
  },
  telClose() {
    this.setData({
      telBlockState: false,
      show:false
    })
    this.load();
  },
  apply() {
    this.setData({
      applyMsgState: false
    })
  },
  copyUrl(e) {
    wx.setClipboardData({
      data: e.target.dataset.url,
      success: function(res) {
        app.showToast('复制成功')
      }
    })
  },
  back() {
    this.setData({
      applyMsgState: true
    })
  },
  onLoad: function() {
    let that = this;
    app.getUserInfoFirst().then(function(res) {
      if (res.status == 200) {
        if (wx.getStorageSync("status") != 200) {
          that.setData({
            show: true
          })
        } else if (wx.getStorageSync("status") == 200) {
          that.setData({
            show: false
          })
          that.load();
        }
      }
    })
  },
  onReachBottom: function(e) {
    this.loadMore()
  }
})